package com.liugl.activitylifecycletest

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import utils.L

class MainActivity : AppCompatActivity() {
    private val tag = "[MainActivity]"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        L.d("${tag}onCreate")
        setContentView(R.layout.activity_main)

        sn.setOnClickListener {
            val intent = Intent(this, NormalActivity::class.java)
            val b = Bundle()
            b.putString("s_key", "Hello NormalActivity")

            intent.putExtra("data", b)

            startActivityForResult(intent, 1)
//            startActivity(intent)
        }

        sd.setOnClickListener {
            val intent = Intent(this, DialogActivity::class.java)
            startActivity(intent)
        }

//        if (savedInstanceState != null) {
//            val tempData = savedInstanceState.getString("data_key")
//            L.d("$tempData")
//        }

    }

    override fun onStart() {
        super.onStart()
        L.d("${tag}onStart")
    }

    override fun onResume() {
        super.onResume()
        L.d("${tag}onResume")
    }

    override fun onPause() {
        super.onPause()
        L.d("${tag}onPause")
    }

    override fun onStop() {
        super.onStop()
        L.d("${tag}onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        L.d("${tag}onDestroy")
    }

    override fun onRestart() {
        super.onRestart()
        L.d("${tag}onRestart")
    }

    //Activity被回收前一定会调用
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

        L.d("onSaveInstanceState")

        val tempData = "Something you just typed"
        outState.putString("data_key", tempData)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)



        if (resultCode == RESULT_OK) {
            L.d("back========")

            when (requestCode) {
                1 -> {
                    val result = data?.getBundleExtra("re")
                    L.d("${result?.getString("result")}")
                }
            }
        }
    }
}